<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body >
  <input type="text" id="test">
</body>
</html>
<script>
  document.querySelector("#test").focus()
  document.addEventListener("visibilitychange", function() {
  console.log( document.visibilityState );
  console.log(document.hidden);
  if(document.hidden) {
    document.title = "被隐藏了！"
  } else {
    document.title = "又回来了~"
  }
});
window.onbeforeunload = function()
{
    setTimeout(()=> {
      alert("取消离开");
    }, 10);
    return "真的离开?";
}
window.onunloadcancel = function()
{
    alert("取消离开");
}


</script>